﻿using WindowsForms.AOnline.Model.BusinessObjects.Dictionary;
using WindowsForms.AOnline.Presenter.Dictionary.Account;
using WindowsForms.AOnline.View.Dictionary;
//using System.Threading.Tasks;

namespace WindowsForms.AOnline.Presenter.Dictionary.AutoBusiness
{
    public class AutoBusinessPresenter : Presenter<IAutoBusinessView>  
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AccountPresenter" /> class.
        /// </summary>
        /// <param name="view">The view.</param>
        public AutoBusinessPresenter(IAutoBusinessView view)
            : base(view)
        {
        }

        /// <summary>
        /// Displays the specified employ identifier.
        /// </summary>
        /// <param name="autoBusinessId">The employ identifier.</param>
        public void Display(string autoBusinessId) 
        {
            if (autoBusinessId == null) { View.AutoBusinessId = 0; return; }
            var autoBusiness = Model.GetAutoBusiness(int.Parse(autoBusinessId));
            View.AutoBusinessId = autoBusiness.AutoBusinessId;
            View.AutoBusinessCode = autoBusiness.AutoBusinessCode;
            View.AutoBusinessName = autoBusiness.AutoBusinessName;
            View.RefTypeId = autoBusiness.RefTypeId;
            View.VoucherTypeId = autoBusiness.VoucherTypeId;
            View.DebitAccountNumber = autoBusiness.DebitAccountNumber;
            View.CreditAccountNumber = autoBusiness.CreditAccountNumber;
           // View.BudgetItemCode = autoBusiness.BudgetItemCode;
          //  View.BudgetSourceCode = autoBusiness.BudgetSourceCode;
            View.Description = autoBusiness.Description;
            View.IsActive = autoBusiness.IsActive;
            View.UnitId = autoBusiness.UnitId;
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        /// <returns>System.Int32.</returns>
        public string Save()
        {
            var autoBusiness = new AutoBusinessModel     
            {
                AutoBusinessId = View.AutoBusinessId,
                AutoBusinessCode = View.AutoBusinessCode,
                AutoBusinessName = View.AutoBusinessName,
                RefTypeId = View.RefTypeId,
                VoucherTypeId = View.VoucherTypeId,
                DebitAccountNumber = View.DebitAccountNumber,
                CreditAccountNumber = View.CreditAccountNumber,
             //   BudgetItemCode = View.BudgetItemCode,
             //   BudgetSourceCode = View.BudgetSourceCode,
                Description = View.Description,
                IsActive = View.IsActive,
                UnitId = View.UnitId
            };

            if (View.AutoBusinessId == 0)
                return Model.AddAutoBusiness(autoBusiness);
            return Model.UpdateAutoBusiness(autoBusiness);
        }

        /// <summary>
        /// Deletes the specified employ identifier.
        /// </summary>
        /// <param name="employId">The employ identifier.</param>
        /// <returns>System.Int32.</returns>
        public string Delete(int employId) 
        {
            return Model.DeleteAutoBusiness(employId);
        }
    }
}
